<?php
include("config/session.php");
include("./config/config.inc.php");
include("./config/QuickMySQL.php");
include("./config/function.inc.php");

class CSendMailAdd extends PageBase
{
	/* 加载页面 */
	function OnPageLoad(){
		include("./class/template.inc");
		
		$this->mysql = new QuickMySQL();
		$tpl = new Template("./ui/");
		
		// 显示用户组
		$tpl->set_file("main", "sendemail-add.html");
		$tpl->set_unknowns("keep");
		$tpl->set_block("main", "group_list", "s_group_list");
		$res = $this->mysql->Query("select sname,tname from t_list_user");
		while( $row = mysql_fetch_row($res) ){
			$tpl->set_var("tb", substr($row[1], 7));
			$tpl->set_var("glname", $row[0]);
			$tpl->parse("s_group_list", "group_list", true);
		}
		mysql_free_result($res);
		// 到期提醒队列
		$tpl->set_block("main", "group_list2", "s_group_list2");
		$res = $this->mysql->Query("select sname,tname from t_list_user where tname not in (select sm_to from t_sendemail_notify)");
		while( $row = mysql_fetch_row($res) ){
			$tpl->set_var("tb", substr($row[1], 7));
			$tpl->set_var("glname", $row[0]);
			$tpl->parse("s_group_list2", "group_list2", true);
		}
		mysql_free_result($res);
		// 显示邮件列表
		$tpl->set_block("main", "email_list", "s_email_list");
		$res = $this->mysql->Query("select id, addr from t_email_list");
		while( $row = mysql_fetch_row($res) ){
			$tpl->set_var("id", $row[0]);
			$tpl->set_var("email", $row[1]);
			$tpl->parse("s_email_list", "email_list", true);
		}
		mysql_free_result($res);
		// 构建发送编号
		$tpl->set_block("main", "send_no", "s_send_no");
		$res = $this->mysql->Query("select sm_flag from t_sendemail_group where sm_flag > 0 and sm_flag < 32 order by sm_flag asc");
		$row = mysql_fetch_row($res);
		for($i = 1; $i < 32; $i++){
			if( $row && $row[0] == $i){
				$row = mysql_fetch_row($res);
				continue;
			}
			$tpl->set_var("no", $i);
			$tpl->parse("s_send_no", "send_no", "no");
		}
		mysql_free_result($res);
		// 输出显示
		$tpl->pparse("mains", "main");
		
	}
	/* 处理请求 */
	function OnRequest(){
		echo "<meta http-equiv=\"content-type\" content=\"text/html;charset=uft-8\">";
		
		$users = array("vm" => "t_user_vm", "qq" => "t_user_qq");
		$title = $_POST['title'];
		$body = eregi_replace("[\](.)",'\\1',$_POST['sendemail_preview']);//base64_encode($_POST['sendemail_preview']);
		$body = str_replace("'", "''", $body);
		$user = "";
		foreach( $users as $ut => $tt ){
			if( !isset($_POST["utype_" . $ut]) )
				continue;
			if( $ut != $_POST["utype_" . $ut])
				continue;
			if( "" != $user ) $user .= ";";
			$user .= $tt;
		}
		if("" == $user){
			echo "<script language='javascript'>alert('发送的用户为空'); location = 'sendemail.php'</script>";
			return;
		}
		// 添加到发送队列
		$sql = "insert into t_sendmail_queue(sm_subject,sm_to,sm_body) values("
			. "'" . str_replace("'", "''", $title) . "'"
			. ",'" . str_replace("'", "''", $user) . "'"
			. ", '{$body}')";
		if( !QuickMySQL::quickExecute($sql) ){
			echo "<script language='javascript'>alert('添加到发送队列失败'); location = 'sendemail.php'</script>";
			return;
		}
		// 启动新线程
		$fp = fsockopen($_SERVER['SERVER_ADDR'], $_SERVER['SERVER_PORT'], $errno, $error);
		if( $fp ){
			fputs($fp, "GET /class/sendemail-s.php\r\n");
			fputs($fp, "Host: {$_SERVER['HTTP_HOST']}\r\n\r\n");
			fclose($fp);
			echo "<script language='javascript'>location = 'sendemail.php'</script>";
			sleep(1); // 等等进程启动
		} else {
			echo "<script language='javascript'>alert('启动发送邮件失败'); location = 'sendemail.php'</script>";
		}
	}
}

set_time_limit(0); // 设置超时间
$cc = new CSendMailAdd();
?>